package com.das.mechanic_base.adapter.create;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.DetectionNewBean;
import com.das.mechanic_base.utils.MyImageSpan;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.widget.Watermark;
import com.kproduce.roundcorners.RoundRelativeLayout;

import java.util.ArrayList;
import java.util.List;

public class ServiceInfoFirstAdapter extends RecyclerView.Adapter<ServiceInfoFirstAdapter.ServiceInfoFirstHolder> {

    private List<DetectionNewBean> mList;

    private Context mContext;

    public static Bitmap signBitmap;

    public ServiceInfoFirstAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
    }

    public void changeSerivceInfoFirst(List<DetectionNewBean> mList) {
        this.mList = mList;
        signBitmap = null;
        notifyDataSetChanged();
    }

    public void changeSerivceInfoFirst() {
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ServiceInfoFirstHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ServiceInfoFirstHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_service_info_first_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ServiceInfoFirstHolder holder, int position) {

        holder.v_line.setVisibility(position == mList.size() - 1 ? View.INVISIBLE : View.VISIBLE);

        holder.tv_title.setText(mList.get(position).toSystem);

        String content = "";

        SpannableStringBuilder lightSpan = new SpannableStringBuilder();

        String questionToSystemSn = mList.get(position).questionToSystemSn;

        if (!X3StringUtils.isListEmpty( mList.get(position).questionToItemDetectionFirstVOList)){
            for (DetectionNewBean.QuestionToItemDetectionBean contentBean : mList.get(position).questionToItemDetectionFirstVOList) {
            String itemName = X3StringUtils.isEmpty(contentBean.itemName) ? "" : contentBean.itemName + " ";
            List<DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean> questionList = contentBean.questionList;

            if (X3StringUtils.isListEmpty(questionList)) {
                continue;
            }
            if ("sys_feature_new_001".equals(questionToSystemSn)) {

                String questionAnswer = "";
                for (DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean questionBean : questionList) {
                    if (X3StringUtils.isEmpty(questionBean.answer)) {
                        continue;
                    }
                    String[] answer = questionBean.answer.split("\\. ");
                    if (answer == null || answer.length <= 1) {
                        continue;
                    }

                    if (X3StringUtils.isEmpty(questionAnswer)) {
                        questionAnswer = answer[1];
                    }else {
                        questionAnswer = questionAnswer + ", " +  answer[1];
                    }
                }

                if (X3StringUtils.isEmpty(content)) {
                    content = itemName + "- " + questionAnswer;
                }else {
                    content = content + ", " + itemName + "- " + questionAnswer;
                }

            }else if ("sys_light_new_001".equals(questionToSystemSn)) {

                for (DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean questionBean : questionList) {
                    String answer = questionBean.answer;

                    int length = lightSpan.length();

                    lightSpan.append(answer + "   ");

                    MyImageSpan imageSpan = new MyImageSpan(mContext,X3StringUtils.getLightSpan(answer));

                    lightSpan.setSpan(imageSpan,length,length + answer.indexOf("_") + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }

            }else if ("sys_thing_001".equals(questionToSystemSn) || "sys_tools_001".equals(questionToSystemSn)) {

                for (DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean questionBean : questionList) {
                    if (!questionBean.selected) {
                        continue;
                    }
                    if (X3StringUtils.isEmpty(content)) {
                        content = questionBean.answer;
                    }else {
                        content = content + ", " + questionBean.answer;
                    }
                }

            }else {
                for (DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean questionBean : questionList) {
                    if (X3StringUtils.isEmpty(content)) {
                        content = questionBean.answer;
                    }else {
                        content = content + ", " + questionBean.answer;
                    }
                }
            }
        }
        }

        if ("sys_oil_001".equals(questionToSystemSn)) {
            if (!X3StringUtils.isEmpty(content)){
                double v = Double.parseDouble(content);
                int oilContent = (int) (8 * v / 100);
                if (oilContent == 8) {
                    holder.tv_content.setText(mContext.getString(R.string.x3_full_oil));
                }else {
                    int maxCommon = X3StringUtils.getMaxCommon(oilContent, 8);
                    holder.tv_content.setText((oilContent / maxCommon) + "/" + (8 / maxCommon));
                }
                holder.pb_bar.setVisibility(View.VISIBLE);
                holder.pb_bar.setProgress((int) v);
            }
        } else {
            holder.pb_bar.setVisibility(View.GONE);
            if ("sys_light_new_001".equals(questionToSystemSn)) {
                holder.tv_content.setText(lightSpan);

            } else {
                holder.tv_content.setText(content);
            }
        }

        switch (questionToSystemSn) {
            case "sys_oil_001":
                // 进厂油量
                holder.iv_icon.setImageResource(R.mipmap.service_info_oil);

                break;
            case "sys_light_new_001":
                // 故障灯
                holder.iv_icon.setImageResource(R.mipmap.service_info_light);
                break;
            case "sys_thing_001":
                // 贵重物品
                holder.iv_icon.setImageResource(R.mipmap.service_info_thing);
                break;
            case "sys_feature_new_001":
                // 外观损伤
                holder.iv_icon.setImageResource(R.mipmap.service_info_car);
                break;
            case "sys_tools_001":
                // 随车工具
                holder.iv_icon.setImageResource(R.mipmap.service_info_tool);
                break;
            case "sys_remark_001":
                // 车主备注
                holder.iv_icon.setImageResource(R.mipmap.service_info_note);
                break;
            case "sys_sign_001":
                // 车主签名
                holder.iv_icon.setImageResource(R.mipmap.service_info_sign);
                break;
        }

        if ("sys_sign_001".equals(questionToSystemSn)){
            holder.rl_sign.setVisibility(View.VISIBLE);
            holder.tv_content.setVisibility(View.GONE);
            holder.currentType = "sys_sign_001";
            holder.url = mList.get(position).url;
            holder.storeName = mList.get(position).watermark;

            if (X3StringUtils.isEmpty(mList.get(position).url)){
                holder.iv_sign.setVisibility(View.GONE);
                holder.rrl_water.setVisibility(View.GONE);
                holder.ll_tip.setVisibility(View.VISIBLE);
            } else {
                holder.iv_sign.setVisibility(View.VISIBLE);
                holder.ll_tip.setVisibility(View.GONE);
                X3GlideNewUtils.loadNormalImage2(mContext, X3StringUtils.getImageUrl(mList.get(position).url),holder.iv_sign,0);
            }
        }else{
            holder.rl_sign.setVisibility(View.GONE);
            holder.tv_content.setVisibility(View.VISIBLE);
            holder.currentType = "";
            holder.url = "";
        }

    }

    IOnClickJumpFirst iOnClickJumpFirst;

    public ServiceInfoFirstAdapter setiOnClickJumpFirst(IOnClickJumpFirst iOnClickJumpFirst) {
        this.iOnClickJumpFirst = iOnClickJumpFirst;
        return this;
    }

    public interface IOnClickJumpFirst {
        void iOnClickJumpFirst();
        void iOnClickToSign(String url,String storeName);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ServiceInfoFirstHolder extends RecyclerView.ViewHolder {
        ImageView iv_icon;
        TextView tv_title;
        TextView tv_content;
        ProgressBar pb_bar;
        View v_line;
        RelativeLayout rl_sign;
        ImageView iv_sign;
        LinearLayout ll_tip;
        RoundRelativeLayout rrl_water;

        public String currentType;
        public String url;
        public String storeName;

        public ServiceInfoFirstHolder(@NonNull View itemView) {
            super(itemView);
            iv_icon = itemView.findViewById(R.id.iv_icon);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_content = itemView.findViewById(R.id.tv_content);
            pb_bar = itemView.findViewById(R.id.pb_bar);
            v_line = itemView.findViewById(R.id.v_line);
            rl_sign = itemView.findViewById(R.id.rl_sign);
            iv_sign = itemView.findViewById(R.id.iv_sign);
            ll_tip = itemView.findViewById(R.id.ll_tip);
            rrl_water = itemView.findViewById(R.id.rrl_water);

            itemView.setOnClickListener(v -> {
                if (iOnClickJumpFirst != null) {
                    if ("sys_sign_001".equals(currentType)){
                        iOnClickJumpFirst.iOnClickToSign(url,storeName);
                    } else {
                        iOnClickJumpFirst.iOnClickJumpFirst();
                    }
                }
            });
        }
    }
}
